package com.comleader.uap.service;

import static cn.hutool.core.date.DateUtil.formatDateTime;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.comleader.uap.common.response.Result;
import com.comleader.uap.common.util.ExcelUtil;
import com.comleader.uap.common.util.ResponseUtil;
import com.comleader.uap.mapper.UrlBlockSyncMapper;
import com.comleader.uap.pojo.entity.UrlBlockSync;
import com.comleader.uap.pojo.query.UrlBlockQuery;
import com.comleader.uap.pojo.vo.UrlBlockDetailVO;
import com.comleader.uap.pojo.vo.UrlBlockListVO;
import com.comleader.uap.pojo.vo.UrlPushVO;

import cn.hutool.core.collection.CollUtil;

/**
 * @author lianxinjing
 * @description 针对表【NOG_URL_BLOCK_SYNC(网址封堵推送表结构)】的数据库操作Service
 * @createDate 2024-11-23 16:23:00
 */
@DS("oracle")
@Service
public class UrlBlockSyncService extends ServiceImpl<UrlBlockSyncMapper, UrlBlockSync> {

    @Resource
    private UrlBlockFeedbackSyncService urlBlockFeedbackSyncService;
    @Resource
    private UrlUnblockFeedbackSyncService urlUnblockFeedbackSyncService;

    public List<UrlBlockListVO> selectList(UrlBlockQuery query) {
        if (CollUtil.isNotEmpty(query.getIssueTime())) {
            query.setIssueStartTime(query.getIssueTime().get(0));
            query.setIssueEndTime(query.getIssueTime().get(1));
        }
        List<UrlBlockListVO> list = baseMapper.selectList(query);
        return list;
    }

    public UrlBlockDetailVO selectDetail(String seq) {
        List<UrlBlockDetailVO.BlockFeedback> blockList = urlBlockFeedbackSyncService.selectList(seq);
        List<UrlBlockDetailVO.UnblockFeedback> unblockList = urlUnblockFeedbackSyncService.selectList(seq);
        return new UrlBlockDetailVO(blockList, unblockList);
    }

    public void export(UrlBlockQuery query, HttpServletResponse response) throws IOException {
        List<Date> issueTime = query.getIssueTime();
        if (CollUtil.isEmpty(issueTime) ||issueTime.size() != 2) {
            ResponseUtil.write(response, Result.fail("必须选择开始时间和结束时间"));
            return;
        }
        try {
            String fileName = "网址封堵_" + formatDateTime(issueTime.get(0)) + "_" + formatDateTime(issueTime.get(1)) + ".xlsx";
            List<UrlBlockListVO> list = selectList(query);
            ExcelUtil.exportMultipleSheet(response, fileName, UrlBlockListVO.class, "网址封堵", list);
        } catch (Exception e) {
            log.error("网址封堵导出异常", e);
            ResponseUtil.write(response, Result.fail("系统异常"));
        }

    }

}
